package com.bankcomm.location;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.bankcomm.R;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.LocationServiceActivity;
import com.bankcomm.util.DensityUtil;

/**
 * 自定义点OverlayItem类
 * 
 * @author zhang.h
 * 
 * @date 2012-6-6 上午09:35:18
 */
public class MyPoiOverlayItem extends ItemizedOverlay<OverlayItem> {
	public ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	public LocationServiceActivity mActivity;
	public Drawable marker;
	
	public MyPoiOverlayItem(LocationServiceActivity activity, Drawable marker) {
		super(boundCenterBottom(marker));
		this.mActivity = activity;
		this.marker = marker;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
		// 调整一个drawable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
		boundCenterBottom(marker);
	}

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	// 处理当点击事件
	@Override
	protected boolean onTap(int i) {
		OverlayItem currOI = mOverlays.get(i);
		View currPopView = this.mActivity.mPopView;

		// 名称
		TextView titleText = (TextView) currPopView.findViewById(R.id.pop_title);
		titleText.setText(currOI.getTitle());

		// 地址
		String snippet = currOI.getSnippet();
		String[] snippetArr = snippet.split("\\|");

		String messageText = null;
		// 多网点
		if (snippetArr.length > 1) {
			messageText = snippetArr[snippetArr.length - 2];
			LPMid.getInstance().currentBranchBody = snippetArr[snippetArr.length - 1];
		}
		// 单网点
		else {
			messageText = snippetArr[0];
		}
		TextView addressText = (TextView) currPopView.findViewById(R.id.pop_address);
		addressText.setText(messageText);

		setFocus(currOI);
		GeoPoint pt = currOI.getPoint();
		MapView.LayoutParams params = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, pt,
				MapView.LayoutParams.BOTTOM_CENTER);
		this.mActivity.mMapView.updateViewLayout(currPopView, params);
		// 移动至中心
		this.mActivity.mMapView.getController().animateTo(pt);

		int xoff = DensityUtil.dip2px(mActivity, 1.2f);
		int yoff = DensityUtil.dip2px(mActivity, 30f);
		params.x -= xoff;
		params.y -= yoff;

		currPopView.setVisibility(View.VISIBLE);
		// **重要**，否则popView会被地图给遮盖
		currPopView.bringToFront();
		return true;
	}

	// 消去弹出的气泡
	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		this.mActivity.mPopView.setVisibility(View.GONE);
		return super.onTap(p, mapView);
	}

}
